Copper.DaySchedule = function(config){
    this.data = config.data;
    this.container = config.container;
    this.date = config.date;
    
    this.init();
    this.render();
};

Copper.DaySchedule.prototype = {
    init: function(ct){
        var oThis = this;
        var page = document.createElement('DIV');
        var date = new Date();
        date.setFullYear(this.date.slice(0, 4), this.date.slice(4, 6), this.date.slice(6));
        page.innerHTML = '<div class="copper-todo-header" style="color:white;font-weight:bold;font-size:24px;">' +
            '<div style="">' + date.format('l') + ', </div>' +
            '<div style="">' + date.format('F d') + '</div>' +
          '</div>' +
          '<div class="copper-todo-body"><ul></ul></div>';
        page.className = 'copper-schedule-page';
        
        this.container.appendChild(page);
        this.el = page;
        
        var body = page.querySelector('.copper-todo-body');
       
        body.addEventListener('click', function(e){oThis.onBodyClick(e);}, false); 
    },
    
    render: function(){
        var el = this.el;
        el.querySelector('.copper-todo-body ul').innerHTML = ''; 
        for(var i = 0; i < this.data.length; ++i){
            var item = document.createElement('LI');
            item.innerHTML = '<div class="copper-todo-time">' + this.data[i].startTime.slice(9) + 
                ' ~ ' + this.data[i].endTime.slice(9) + '</div>' + 
                this.data[i].content;
            item.className = 'copper-todo-item';
            item.setAttribute('data-id', this.data[i].id);
            el.querySelector('.copper-todo-body ul').appendChild(item);
            //el.querySelector('.copper-todo-index').innerHTML = i + 1 + ' / ' + pageCount;
        }
    },
    
    setHeight: function(height){
        this.el.querySelector('.copper-todo-body').style.height = height + 'px';    
    },
    
    onBodyClick: function(e){
        var target = e.target;
        if(target.nodeName != 'LI'){
            target = target.parentNode;  
            if(target.nodeName != 'LI'){
                return;
            }  
        }
        
        var body = this.el.querySelector('.copper-todo-body');
        /*
        var last = body.querySelector('.copper-todo-selected');
        if(last){
            last.className = 'copper-todo-item';
        }
        */
        Copper.TaskSchedule.reset();
        target.className = 'copper-todo-selected';
        this.selectedItem = target.getAttribute('data-id');
        
        var deleteBtn = this.deleteBtn;
        var oThis = this;
        if(!deleteBtn){
            deleteBtn = document.createElement('DIV');
            deleteBtn.className = 'copper-todo-delete';
            deleteBtn.selectable = false;
            deleteBtn.addEventListener('click', function(){oThis.removeItem();}, false);
            body.appendChild(deleteBtn);
            this.deleteBtn = deleteBtn;
        }
        deleteBtn.style.left = target.offsetLeft + target.offsetWidth - 28 + 'px';
        deleteBtn.style.top = target.offsetTop + 'px';
        deleteBtn.style.display = 'block';
            
    },
    
    removeItem: function(){     
        for(var i = 0; i < this.data.length; ++i){
            if(this.data[i].id + '' == this.selectedItem){
                this.data.splice(i, 1);
                this.render();
                break;
            }
        }

        if(this.date == Copper.Global.currentDate){
            Copper.TaskManager.setData(this.data);
            Copper.TaskManager.refresh();
        }

        this.deleteBtn.style.display = 'none';
    },
    
    resetStatus: function(){
        this.selectedItem = null;
        this.deleteBtn.style.display = 'none';
    }
};